package com.shu.qq.config.security.handler;

import com.alibaba.fastjson.JSON;
import com.shu.qq.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Slf4j
public class UserAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.info("登录认证失败，异常处理{}",authException.getMessage());
        R<String> failed = new R<>(401, "登录认证失败，账号或密码错误");
        String s = JSON.toJSONString(failed);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(s);
    }

}
